// pages/my-balance-detail/my-balance-detail.js
import {Balance} from '../../../models/balance'
import {check} from '../../../util/identityCheck'

const balanceModel = new Balance()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        type: 0,
        data: {},
        balanceDetail: [],
        tabInfo: ['全部', '收入', '支出'],
        page: 1,
        showLoading: true
    },

    onChange(event) {
        this.setData({
            type: event.detail.index,
            page: 1,
            balanceDetail: [],
            showLoading: true
        })
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })
        this._GetMyBalance()
    },

    recharge() {
        wx.showToast({
            title: '请去app充值',
            icon: 'none'
        });
    },

    cash() {
        wx.navigateTo({
            url: '/pages/my-balance/my-balance'
        })
    },

    _GetMyBalance() {
        let params = {
            type: this.data.type,
            pagesize: 10,
            page: this.data.page
        }
        balanceModel.getMyBalance(params, res => {
            console.log(res.balance_detail_list)

            this.data.balanceDetail.push(...res.balance_detail_list)
            console.log(this.data.balanceDetail)
            this.setData({
                data: res,
                balanceDetail: this.data.balanceDetail
            })
            if (this.data.balanceDetail.length % 10 == 0 && res.balance_detail_list.length !== 0) {
                this.setData({
                    showLoading: true
                })
            }else{
                this.setData({
                    showLoading: false
                })
            }
            console.log(this.data.balanceDetail);


        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(!check())return
        this._GetMyBalance()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.balanceDetail.length % 10 === 0) {
            this.setData({
                page: this.data.page + 1
            })
            this._GetMyBalance()
        }
    }
})